
var config = require("config");
var CardUtil = require("CardUtil");

cc.Class({
    extends: cc.Component,

    properties: {
        pokerNum : {
            default : null,
            type : cc.Sprite
        },

        pokerType : {
            default : null,
            type : cc.Sprite
        },

        pokerType2 : {
            default : null,
            type : cc.Sprite
        },

        pokerBg : {
            default : null,
            type : cc.Sprite
        },
        pokerLight : {
            default : null,
            type : cc.Sprite
        },

        pokerDiZhu : {
            default : null,
            type : cc.Sprite
        },
        pokerShow : {
            default : null,
            type : cc.Sprite
        },

        canTouch : false,
        isTouched : false,
        cardData : {
            default : null,
        },
        isTopped:false,//是否被提起
    },
    //展示poker
    showPoker(showData){
        var self = this;
        self.cardData = showData;
        var showType = showData.showType;
        self.canTouch = showData.canTouch;

        self.cardValue = showData.showTxt;
        var numUrl = "cards/poke_" + self.cardValue;
       
        if(self.cardValue!=16&&self.cardValue!=17){
            self.pokerType2.enabled = true;
            self.pokerNum.enabled = true;
            cc.loader.loadRes(numUrl,cc.SpriteFrame,function(err,spriteFrame){
                self.pokerNum.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            });
        }
        
        // console.log(""+showData.showTxt+":"+self.cardValue);
        // self.pokerType2.enabled = true;
        // self.pokerNum.enabled = true;
        
        self.pokerDiZhu.enabled = false;
        self.pokerLight.enabled = false;
        self.pokerShow.enabled = false;
        self.pokerType.enabled = true;
        self.pokerType2.enabled = true;

        var imgUrl = "cards/heitao";
        if(showType == config.pokerCardType.spade){
            imgUrl = "cards/heitao";
            self.pokerNum.node.color = new cc.Color(27, 27, 27);
        }else if(showType == config.pokerCardType.hearts){
            imgUrl = "cards/hongxin";
            self.pokerNum.node.color = new cc.Color(246, 58, 27);
        }else if(showType == config.pokerCardType.redslice){
            imgUrl = "cards/hongfang";
            self.pokerNum.node.color = new cc.Color(246, 58, 27);
        }else if(showType == config.pokerCardType.blackberry){
            imgUrl = "cards/heimei";
            self.pokerNum.node.color = new cc.Color(27, 27, 27);
        }else if(showType == config.ghostCardType.bigG){
            self.pokerType.enabled = false;
            self.pokerType2.enabled = false;
            self.pokerNum.enabled = false;
            cc.loader.loadRes("cards/joker_red",cc.SpriteFrame,function(err,spriteFrame){
                self.pokerBg.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            })
        }else if(showType == config.ghostCardType.smallG){
            self.pokerType.enabled = false;
            self.pokerType2.enabled = false;
            self.pokerNum.enabled = false;
            cc.loader.loadRes("cards/joker_black",cc.SpriteFrame,function(err,spriteFrame){
                self.pokerBg.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            })
        }
        var jokerValue = CardUtil.serverCardValueToClient(config.joker);
        if(self.cardValue == jokerValue){
            imgUrl = "cards/laizi";
            self.pokerNum.node.color = new cc.Color(221, 109, 30);
            // console.log("numUrl:"+numUrl);
        }
        cc.loader.loadRes(imgUrl,cc.SpriteFrame,function(err,spriteFrame){
            self.pokerType.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            self.pokerType2.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        })
    },
    setPokerLight(isVisible){
        this.pokerLight.enabled = isVisible;
    },
    setChoosed(isTouched){ 
        this.cardData.isChoosed = isTouched;
        this.pokerLight.enabled = isTouched;
    },
    cardMove(){
        if(this.cardData.isChoosed&&this.cardData.isTopped){
            // this.node.
            var move = cc.moveBy(0.1,0,-30);
            move.easing(cc.easeIn(3));
            this.node.runAction(move);
            this.cardData.isTopped = false;
            this.cardData.isChoosed = false;
            this.pokerLight.enabled = false;
        }else if(this.cardData.isChoosed&&!this.cardData.isTopped){
            var move = cc.moveBy(0.1,0,30);
            move.easing(cc.easeIn(3));
            this.node.runAction(move);
            this.cardData.isTopped = true;
            this.cardData.isChoosed = false;
            this.pokerLight.enabled = false;
        }
    },
    cardMoveDown(){
        if(this.cardData.isTopped){
            var move = cc.moveBy(0.1,0,-30);
            move.easing(cc.easeIn(3));
            this.node.runAction(move);
            this.cardData.isTopped = false;
            this.cardData.isChoosed = false;
            this.pokerLight.enabled = false;
        }
    },
    setCardDiZhu(isEnabled){
        this.pokerDiZhu.enabled = isEnabled;
    },
    setCardShow(isEnabled){
        this.pokerShow.enabled = isEnabled;
    },
    setMoveDown(){
        this.cardData.isTopped = false;
        this.cardData.isChoosed = false;
        this.pokerLight.enabled = false;
    },
    setPokerBg(){
        var self = this;
        cc.loader.loadRes("cards/blank_card",cc.SpriteFrame,function(err,spriteFrame){
            self.pokerBg.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        })
    },
    setScale(scale){
        this.node.scaleX = scale;
        this.node.scaleY = scale;
    },
    convertLazarillo(cardValue){
        var self = this;

        self.pokerDiZhu.enabled = false;
        self.pokerLight.enabled = false;
        self.pokerShow.enabled = false;
        self.pokerType.enabled = true;
        self.pokerType2.enabled = true;

        var numUrl = "cards/poke_" + cardValue;
        cc.loader.loadRes(numUrl,cc.SpriteFrame,function(err,spriteFrame){
            self.pokerNum.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            // console.log("convertLazarillo:"+numUrl);
        });

        var imgUrl = "cards/laizi";
        self.pokerNum.node.color = new cc.Color(221, 109, 30);
        cc.loader.loadRes(imgUrl,cc.SpriteFrame,function(err,spriteFrame){
            self.pokerType.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            self.pokerType2.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        })
    }
});
